# -*- coding: utf-8 -*-
import sys,os,re
from pipe import Pipe
import threading
from req import  Req
import Util#Util.getDict
import random
from factoryReq import  ReqFactory
from mongoBase import MongoDBBase
class Fetcher(threading.Thread):
    def __init__(self,fetcher_name,user_to_fetcher_pipe,fetcher_to_unit_pipe,DB_conn):
        threading.Thread.__init__(self, name=fetcher_name)
        self.fetcher_to_unit_pipe = fetcher_to_unit_pipe
        self.user_to_fetcher_pipe = user_to_fetcher_pipe
        self._DB_conn=DB_conn
    def run(self):
        while True:
            pipe_is_empty=self.user_to_fetcher_pipe.empty()
            if pipe_is_empty is True:
                print("######Thread %s is empty ,break!"%self.getName())
                break
            req=self.user_to_fetcher_pipe.get()
            print("######Thread %s START a job,processed Req: %d" % (self.getName(), req._id))
            self.working(req)
            self.fetcher_to_unit_pipe.put(req)
            print("######Thread %s FINISHED a job to processed Req: %d" % (self.getName(),req._id))
        print("######Thread %s exit" % self.getName())

    def working(self,req):
        if (type(req) is not Req):
            raise Exception
        fetch_param_set=req.fetch_info["fetch_param_set"]
        data_info=req.data_info

        if req.unit_info["unit_name"] =="SampleJudge":
            self.get_dataset_to_unit_SampleJudge(fetch_param_set=fetch_param_set,data_info=data_info)
    #todo Warning field cannot find in query
    def extract_doc(self,field_list,raw_list):
        #param raw_list [{"uid":1,"pid":100000001,"tid":1,"ans":1,"ace":0},{}]
        #param field_list ["pid","tid","ans"]
        #return new_list [{"pid":100000001,"tid":1,"ans":1},{}]
        new_list=[]
        for doc in raw_list:
            new_doc={}
            for field in field_list:
                try:
                    new_doc[field] = doc[field]
                except:
                    print("wrong in doc->field: %s"%field)
                    print(doc)
                    exit(1)
            new_list.append(new_doc)
        print("#######get new list:")
        for i in new_list:
            print(i)
        return new_list
    def get_dataset_uid_history_ans(self,uid_list):
        # pid_tid_ans_list do not wrong
        DB_conn=self._DB_conn
        coll=DB_conn.get_coll("uid_pid_tid_ans_ase_coll")
        data_list=[]
        field_list={'pid','tid','ans'}
        for uid in uid_list:
            raw_list=DB_conn.get_doc(coll=coll,doc_filter={"uid":uid})
            print("   ###according to doc_filter--%s,raw_list:"%{"uid":uid})
            #print(raw_list)
            pid_tid_ans_list=self.extract_doc(field_list=field_list,raw_list=raw_list)
            data_list.append(Util.getDict(uid=uid,pid_tid_ans_list=pid_tid_ans_list))
            dataset_uid_history_ans=Util.getDict(dataset_usage="dataset_to_unit",field_name_list=["uid","pid","tid","ans"],data_list_name="pid_tid_ans_list",data_list=data_list)
        return dataset_uid_history_ans
    def get_dataset_pid_history_ans(self,pid_list):
        # tid_uid_ans_list do not wrong
        DB_conn = self._DB_conn
        coll = DB_conn.get_coll("uid_pid_tid_ans_ase_coll")
        data_list=[]
        field_list={'uid','tid','ans'}
        for pid in pid_list:
            raw_list=DB_conn.get_doc(coll=coll,doc_filter={"pid":pid})
            print("   ###according to doc_filter--%s,raw_list:"%{"pid":pid})
            #print(raw_list)
            tid_uid_ans_list=self.extract_doc(field_list=field_list,raw_list=raw_list)
            data_list.append(Util.getDict(pid=pid,tid_uid_ans_list=tid_uid_ans_list))
            dataset_pid_history_ans=Util.getDict(dataset_usage="dataset_to_unit",field_name_list=["uid","pid","tid","ans"],data_list_name="tid_uid_ans_list",data_list=data_list)
        return dataset_pid_history_ans
    def get_dataset_tid_history_ans(self, tid_list):
        #pid_num_list do not wrong
        DB_conn = self._DB_conn
        coll = DB_conn.get_coll("tid_pid_num_coll")
        field_list = {'pid', 'yes_num','no_num'}
        data_list = []
        for tid in tid_list:
            raw_list=DB_conn.get_doc(coll=coll,doc_filter={"tid":tid})
            print("   ###according to doc_filter--%s,raw_list:"%{"tid":tid})
            print(raw_list)
            pid_num_list = self.extract_doc(field_list=field_list,raw_list=raw_list)
            data_list.append(Util.getDict(tid=tid, pid_num_list=pid_num_list))
            dataset_tid_history_ans = Util.getDict(dataset_usage="dataset_to_unit",
            field_name_list=["pid", "tid", "yes_num","no_num"],data_list_name="pid_num_list", data_list=data_list)
        return dataset_tid_history_ans
    def get_dataset_to_unit_SampleJudge(self,fetch_param_set,data_info):
        #param fetch_param_set ->Req.fetch_info["fetch_param_set"]
        #param data_info ->Req.data_info
        print("#######start to get data_info")
        dataset_uid_history_ans=self.get_dataset_uid_history_ans(fetch_param_set["uid_list"])
        dataset_pid_history_ans=self.get_dataset_pid_history_ans(fetch_param_set["pid_list"])
        dataset_tid_history_ans=self.get_dataset_tid_history_ans(fetch_param_set["tid_list"])
        data_info["dataset_uid_history_ans"]=dataset_uid_history_ans
        data_info["dataset_pid_history_ans"] = dataset_pid_history_ans
        data_info["dataset_tid_history_ans"] = dataset_tid_history_ans
        print("#######get data_info succeefully!")
if __name__ == '__main__':

    myReq_list=[]
    for i in range(5):
        input_info={}
        input_info["unit_name"]="SampleJudge"
        input_info["dataset_from_commit"]={}
        input_info["dataset_from_commit"]["uid_pid_tid_ans_list"]=[]
        for j in range(10):
            input_info["dataset_from_commit"]["uid_pid_tid_ans_list"].append(Util.getDict(
                uid=i+1,pid=100000001+j//4,tid=j%4+1,ans=random.choice([-1,0,1]),ase=0))
        print(input_info)
        req=Req(id=i+1,input_info=input_info)
        myReq_list.append(req)

    user_to_fetcher_pipe=Pipe()
    myReqFactory=ReqFactory("myReqFactory",user_to_fetcher_pipe)
    for r in myReq_list:
        myReqFactory.work(r)
    for r in myReq_list:
        print (r.fetch_info)

    myconn = MongoDBBase()
    fetcher_to_unit_pipe = Pipe()
    myFetcher1 = Fetcher("myFetcher1", user_to_fetcher_pipe=user_to_fetcher_pipe,
    fetcher_to_unit_pipe=fetcher_to_unit_pipe, DB_conn=myconn)
    myFetcher1.start()
    myFetcher1.join()

    len=fetcher_to_unit_pipe.get_size()
    idx=0
    while idx<len:
        req=fetcher_to_unit_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now fetcher_to_unit_pipe has %d req left"%fetcher_to_unit_pipe.get_size())
        idx+=1